#!/bin/bash

# 检查 Git 是否正常
if output=$(git status --porcelain) && [ -z "$output" ]; then
    echo "👍 Git is clean, start the next step."
else
    echo "oh my God, An error occurred, please ensure git status clean"
    exit 0
fi

tag=$1
# 执行 git pull
git pull

if [[ $tag == '' ]]; then
    echo "请输入要设置的 tag："
    read tag
fi

echo -e "\n当前tag：$tag"

# 读取整个文件内容
file_content=$(cat TLCLock.podspec)

replacement="    s.version       = \"$tag\""
# 替换版本号
new_content=$(echo "$file_content" | sed '/s.version       =/s/.*/'"$replacement"'/')

# 将替换后的内容写回文件
echo "$new_content" > TLCLock.podspec


FRAMEWORK_PATH="../tlclock_ios/build/TLCLock.xcframework"

if [[ -d "${FRAMEWORK_PATH}" ]]; then
    echo "cp -r ${FRAMEWORK_PATH} ./"
    cp -r "${FRAMEWORK_PATH}" "./"
else
    echo "${FRAMEWORK_PATH} not found"
    exit 1
fi

# 提交更改
git stage .
git commit -m "ci update sdk to v$tag"
git push

# 打 tag
git tag "${tag}"

# 推送 tag
git push origin "${tag}"

# 发布pod
echo "发布Pod，是否继续？(y/n)"
read answer
if [ "$answer" = "y" ]; then
    pod lib lint --allow-warnings
    pod trunk push TLCLock.podspec --allow-warnings
else
    echo "pod未发布"
fi

